package com.photography.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.photography.annotation.RequireRole;
import com.photography.common.Result;
import com.photography.entity.Photographers;
import com.photography.service.PhotographersService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/photographers")
public class PhotographerController {

    @Resource
    private PhotographersService photographersService;

    /**
     * 摄影师登录
     *
     * @param account  账号
     * @param password 密码
     * @return 登录结果
     */
    @PostMapping("/login")
    public Result login(@RequestParam String account, @RequestParam String password) {
        return photographersService.login(account, password);
    }

    /**
     * 摄影师注册
     *
     * @param photographers 摄影师信息
     * @return 结果
     */
    @RequireRole("manager")
    @PostMapping("/register")
    public Result register(@RequestBody Photographers photographers) {
        return photographersService.register(photographers);
    }

    /**
     * 更新摄影师信息
     *
     * @param photographers 摄影师信息
     * @return 结果
     */
    @RequireRole({"manager", "employee"})
    @PutMapping("/update")
    public Result updatePhotographers(@RequestBody Photographers photographers) {
        return photographersService.updatePhotographers(photographers);
    }

    @RequireRole("manager")
    @PostMapping("/resetPassword/{photographerId}")
    public Result resetPassword(@PathVariable Integer photographerId ){
        return photographersService.resetPassword(photographerId);
    }

    /**
     * 删除
     */
    @RequireRole("manager")
    @DeleteMapping("/delete/{photographerId}")
    public Result deletePhotographers(@PathVariable Integer photographerId ){
        return photographersService.deletePhotographers(photographerId);
    }

    /**
     * 分页查询摄影师
     *
     * @param current   当前页码
     * @param size      每页大小
     * @param gender    性别
     * @param search 搜索关键词，包含姓名，联系方式，擅长领域，工作经历
     * @return 结果
     */
    @RequireRole("manager")
    @GetMapping("/page")
    public Result getPhotographersPage(@RequestParam(defaultValue = "1") Integer current,
                                       @RequestParam(defaultValue = "10") Integer size,
                                       @RequestParam(required = false) String gender,
                                       @RequestParam(required = false) String role,
                                       @RequestParam(required = false) String search
    ) {
        // 创建分页对象
        Page<Photographers> page = new Page<>(current, size);
        // 执行分页查询
        Page<Photographers> PhotographersPage = photographersService.getPhotographersPage(page, gender,role,search);
        return Result.success(PhotographersPage, "查询成功");
    }

    /**
     * 模糊搜索
     *
     * @param current 当前页码
     * @param size    每页大小
     * @param keyword 搜索关键词，支持用户名、擅长领域、邮箱、手机号、昵称的模糊匹配
     * @return 结果
     */
    @GetMapping("/search")
    public Result search(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword
    ) {
        // 创建分页对象
        Page<Photographers> page = new Page<>(current, size);
        // 执行分页查询
        Page<Photographers> PhotographersPage = photographersService.search(page, keyword);
        return Result.success(PhotographersPage, "查询成功");
    }
}
